home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / include / glibmm-2.4 / glibmm / dispatcher.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-04-20  |  3.3 KB  |  103 lines

  1. // -*- c++ -*-
  2. #ifndef _GLIBMM_DISPATCHER_H
  3. #define _GLIBMM_DISPATCHER_H
  4.  
  5. /* $Id: dispatcher.h,v 1.3 2004/02/13 16:53:19 teebaum Exp $ */
  6.  
  7. /* Copyright 2002 The gtkmm Development Team
  8.  *
  9.  * This library is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU Library General Public
  11.  * License as published by the Free Software Foundation; either
  12.  * version 2 of the License, or (at your option) any later version.
  13.  *
  14.  * This library is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.  * Library General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU Library General Public
  20.  * License along with this library; if not, write to the Free
  21.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. #include <sigc++/sigc++.h>
  25. #include <glibmm/main.h>
  26.  
  27.  
  28. namespace Glib
  29. {
  30.  
  31. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  32. class DispatchNotifier;
  33. #endif
  34.  
  35. /** Signal class for inter-thread communication.
  36.  * @ingroup Threads
  37.  * Glib::Dispatcher works similar to sigc::signal<void>.  But unlike normal
  38.  * signals, the notification happens asynchronously through a pipe.  This is
  39.  * a simple and efficient way of communicating between threads, and especially
  40.  * useful in a thread model with a single GUI thread.
  41.  *
  42.  * No mutex locking is involved, apart from the operating system's internal
  43.  * I/O locking.  That implies some usage rules:
  44.  *
  45.  * @li Only one thread may connect to the signal and receive notification, but
  46.  * multiple senders are allowed even without locking.
  47.  * @li The GLib main loop must run in the receiving thread (this will be the
  48.  * GUI thread usually).
  49.  * @li The Dispatcher object must be instantiated by the receiver thread.
  50.  * @li The Dispatcher object should be instantiated before creating any of the
  51.  * sender threads, if you want to avoid extra locking.
  52.  *
  53.  * Notes about performance:
  54.  *
  55.  * @li After instantiation, Glib::Dispatcher will never lock any mutexes on its
  56.  * own.  The interaction with the GLib main loop might involve locking on the
  57.  * @em receiver side.  The @em sender side, however, is guaranteed not to lock,
  58.  * except for internal locking in the <tt>%write()</tt> system call.
  59.  * @li All Dispatcher instances of a receiver thread share the same pipe.  That
  60.  * is, if you use Glib::Dispatcher only to notify the GUI thread, only one pipe
  61.  * is created no matter how many Dispatcher objects you have.
  62.  */
  63. class Dispatcher
  64. {
  65. public:
  66.   /** Create new Dispatcher instance using the default main context.
  67.    * @throw Glib::FileError
  68.    */
  69.   Dispatcher();
  70.   /** Create new Dispatcher instance using an arbitrary main context.
  71.    * @throw Glib::FileError
  72.    */
  73.   explicit Dispatcher(const Glib::RefPtr<MainContext>& context);
  74.   ~Dispatcher();
  75.  
  76.   void emit();
  77.   void operator()();
  78.  
  79.   sigc::connection connect(const sigc::slot<void>& slot);
  80.  
  81. private:
  82.   sigc::signal<void> signal_;
  83.   DispatchNotifier*   notifier_;
  84.  
  85.   // noncopyable
  86.   Dispatcher(const Dispatcher&);
  87.   Dispatcher& operator=(const Dispatcher&);
  88.  
  89. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  90.   friend class Glib::DispatchNotifier;
  91. #endif
  92. };
  93.  
  94. /*! A Glib::Dispatcher example.
  95.  * @example thread/dispatcher.cc
  96.  */
  97.  
  98. } // namespace Glib
  99.  
  100.  
  101. #endif /* _GLIBMM_DISPATCHER_H */
  102.  
  103.